<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="flex-1" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="zjm" class="w-1/4">
          <a-input v-model:value="formState.zjm" placeholder="编号/快递方式" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="mb-3">
        <BaseButton type="primary" class="mr-2" @click="onClickAdd()">新增</BaseButton>
        <BaseExport type="运维" name="快递方式" code="00001131" :search-params="formState">导出</BaseExport>
        <!-- <a-button @click="onExport">导出</a-button> -->
      </div>
      <BaseTable :loading="loading" column-code="00001131" :data-source="tableData" :pagination="pagination" :scroll="{ x: 1000 }" fill>
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'statusStr'">
            <a-switch v-model:checked="record.status" checked-value="1" un-checked-value="0" @change="onHandleDeactivate(record, $event)" />
          </template>
          <template v-if="column.dataIndex === 'dj'">
            <div>{{ record.dj ? '￥' + record.dj : '' }}</div>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onClickAdd(record)">修改</a-button>
            <a-divider type="vertical" />
            <BaseButton confirm-text="确认删除？" type="link" danger size="small" @click="onDelete(record)">删除</BaseButton>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="onSuceess" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import BaseButton from '@/components/BaseButton'
import Edit from './Edit'
// import { message } from 'ant-design-vue'
const formRef = ref(null)
const formState = ref({
  zjm: ''
})
const editOpen = ref(false)
const rowData = ref(null)
const { loading, tableData, pagination, getData } = useTable({
  url: '/operation/opex/getDeliveryMethodInfoList',
  immediate: true,
  params: formState.value
})
const onSuceess = () => {
  editOpen.value = false
  getData()
}
const onClickAdd = (row = {}) => {
  rowData.value = row
  editOpen.value = true
}
const onHandleDeactivate = async (record, status) => {
  const url = ref('/operation/opex/doDisableDeliveryMethod')
  if (status === '1') url.value = '/operation/opex/doEnableDeliveryMethod'
  const res = await post(url.value, {
    id: record.id
  })
  if (res.code !== 0) return message.error(res.msg || '操作失败')
  message.success(res.msg)
  getData()
}
//删除
const onDelete = async (record) => {
  const res = await post('/operation/opex/doDelDeliveryMethodInfo', {
    id: record.id
  })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}
</script>

<style lang="less" scoped></style>
